<template>
	<view class="givePresent">
		<view class="header">
			<view class="left">
				<u-search placeholder="搜索学员昵称或手机号" :show-action="false" class="search-input" shape="square" v-model="query.search" @change.stop="changeSearch"></u-search>
			</view>
		<!-- 	<view class="right">
				<view class="size1">
					全选
				</view>
				<image
					:src="isAllChecked?$IMG_URL('/static/member/agreement2.png'):$IMG_URL('/static/member/agreement1.png')"
					mode="" class="agreement" @click="AllChecked"></image>
			</view> -->
		</view>
		<scroll-view scroll-y="true" class="scroll-Y" @scrolltolower="scrolltolower">
			<view class="comlumn_box" v-if="list.length">
				<view v-for="(item,index) in list" :key="index" class="Item">
					<view class="img">
						<image :src="item.avatar?item.avatar:$IMG_URL('/static/user/notlog.png')" mode="aspectFill" class="avater"></image>
						<image
							:src="!item.gender ?$IMG_URL('/static/mannager/girl.png'):$IMG_URL('/static/mannager/boy.png')"
							mode="aspectFill" class="sex"></image>
					</view>
					<view class="right">
						<view class="title">
							{{item.nickname}}
						</view>
					</view>
					<image
						:src="item.checked?$IMG_URL('/static/member/agreement2.png'):$IMG_URL('/static/member/agreement1.png')"
						mode="aspectFill" class="agreement" @click="changeChecked(index)"></image>
				</view>
				<u-loadmore :status="status" />
			</view>
			<empty v-else :content="'会员'"></empty>
		</scroll-view>
		<view class="foot">
			<view class="btn" @click.stop="getstoreMemberGive">
				确认赠送（已选{{ActiveList.length}}位会员）
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import{
		storeMemberList,
		storeMemberGive
	} from '@/api/manager.js'
	import empty from './components/empty.vue'
	export default {
		components: {
			empty
		},
		data() {
			return {
				status: 'loadmore', //当前状态
				isAllChecked: false,
				query:{
					search:'',
					page:1,
				},
				list: [],
				coupon_id:''
			}
		},
		onLoad(e) {
			this.coupon_id = e.coupon_id
			this.getstoreMemberList()
		},
		computed: {
			ActiveList(){
				 return this.list.filter(item => item.checked).map(item => item.user_id);
			}
		},
		methods: {
			getstoreMemberGive(){
				storeMemberGive({
					coupon_id:this.coupon_id,
					user_id_list:this.ActiveList
				}).then(({data:res})=>{
					console.log('res', res);
					if(res.code == 1){
						this.$refs.uToast.show({
							title: res.msg,
							type: 'success',
							icon: false,
						})
					}else{
						this.$refs.uToast.show({
							title: res.msg,
							type: 'error',
							icon: false,
						})
					}
				})
			},
			changeSearch(){
				this.list = []
				this.query.page = 1
				this.getstoreMemberList()
			},
			scrolltolower(){
				this.query.page++
				this.getstoreMemberList()
			},
			getstoreMemberList(){
				storeMemberList(this.query).then(({data:res})=>{
					console.log('res', res);
					if (res.code == 1) {
						this.status = 'loading'
						if (this.query.page > res.data.all_page) {
							console.log('nomore');
							this.status = 'nomore'
							return false
						} else {
							console.log('more');
							this.status = 'more'
							this.list = this.list.concat(res.data.list)
							this.list = this.list.map(item => {
							  return Object.assign({}, item, { checked: false });
							})
						}
					
						console.log('list', this.list);
					}
				})
			},
			AllChecked() {
				this.isAllChecked = !this.isAllChecked
				this.list.forEach(item => {
					item.checked = this.isAllChecked;
				});
			},
			changeChecked(index){
				console.log('111');
				this.$set(this.list[index], 'checked', !this.list[index].checked);
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #e9e9e9;
	}

	.agreement {
		width: 36rpx;
		height: 36rpx;
	}

	.header {
		width: 750rpx;
		height: 114rpx;
		background: #ffffff;
		box-shadow: 0rpx 8rpx 28rpx 0rpx rgba(199, 205, 179, 0.15);
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0 40rpx 0 24rpx;

		.left {
			flex: 1;

			.search-input {
				width: 100%;
			}
		}

		.right {
			display: flex;
			flex-direction: row;
			align-items: center;

			.size1 {
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC-Medium;
				font-weight: 500;
				text-align: left;
				color: #333333;
				margin-right: 12rpx;
			}


		}
	}

	.scroll-Y {
		width: 100%;
		height: 80vh;
		.comlumn_box {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding-top: 24rpx;

			.Item {
				width: 702rpx;
				height: 128rpx;
				background: #ffffff;
				border-radius: 16rpx;
				padding: 0 24rpx;
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-bottom: 20rpx;

				.img {
					width: 80rpx;
					height: 80rpx;
					background: #f6f6f6;
					border-radius: 50%;
					position: relative;
					margin-right: 20rpx;

					.avater {
						width: 100%;
						height: 100%;
						border-radius: 50%;
					}

					.sex {
						position: absolute;
						right: 0;
						bottom: 0;
						width: 26rpx;
						height: 26rpx;
					}
				}

				.right {
					display: flex;
					flex-direction: column;
					flex: 1;

					.title {
						font-size: 32rpx;
						font-family: PingFang SC, PingFang SC-Bold;
						font-weight: 700;
						text-align: left;
						color: #333333;
					}
				}
			}
		}
	}
	.foot{
		position: fixed;
		bottom: 0;
		width: 750rpx;
		height: 160rpx;
		background: #ffffff;
		box-shadow: 0rpx 8rpx 28rpx 0rpx rgba(166,179,194,0.30); 
		display: flex;
		justify-content: center;
		padding-top: 10rpx;
		.btn{
			width: 710rpx;
			height: 88rpx;
			background: #73F0EA;
			border-radius: 16rpx;
			font-size: 34rpx;
			font-family: PingFang SC, PingFang SC-Bold;
			font-weight: 700;
			text-align: center;
			color: #ffffff;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
</style>